package pub.developers.forum.portal.controller.rest;

import cn.hutool.core.exceptions.ExceptionUtil;
import com.github.tobato.fastdfs.domain.fdfs.StorePath;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import pub.developers.forum.api.model.ResultModel;
import pub.developers.forum.common.exception.BizException;
import pub.developers.forum.common.support.LogUtil;
import pub.developers.forum.portal.config.FastdfsUtils;
import pub.developers.forum.portal.support.ResultModelUtil;

import java.io.IOException;

/**
 * @author caihe
 * @date 2021/4/15 15:24
 */
@Api("图片上传测试接口")
@RestController
@RequestMapping("/file")
public class FileUploadController {

    @Autowired
    private FastdfsUtils fastdfsUtil;

    @PostMapping("/upload")
    public ResultModel<String> fileUpload(@RequestParam(value = "image") MultipartFile file) {
        StorePath upload = null;
        try {
            upload = fastdfsUtil.upload(file);
        } catch (Exception e) {
            ExceptionUtil.wrapRuntimeAndThrow("文件上传失败");
        }
        return ResultModelUtil.success(fastdfsUtil.buildFilePathUrl(upload.getFullPath()));
    }

    @DeleteMapping("/delete")
    public ResultModel deleteFile(@RequestParam("path") String path) {
        fastdfsUtil.delete(path);
        return ResultModelUtil.success();
    }

}
